
using System;
using System.IO;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Build.DataBuilders;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.Build.Pipeline;
using File = System.IO.File;

namespace Miya.Hellow.Editor
{
    //AA �ʲ������ű�
    //1 ���� FixMode��ǩ
    //2 �ռ�Local.Remote�����ʲ����===>_RELEASE/Bundle_XXXXX
    public class BuildScriptFixModePrefix : BuildScriptPackedMode//BuildScriptPackedMode
    {
        static string PATH_BUNDLE_OUT => $"_RELEASE/Bundle_{Builder.BUILD_NUM}{DateTime.Now.ToString("(MMdd-HHmm)")}";
        // Ϊÿһ�� group��������+ ���±�ǩ..
        protected override string ConstructAssetBundleName(AddressableAssetGroup assetGroup, BundledAssetGroupSchema schema,
            BundleDetails info, string assetBundleName)
        {
            var bundleNameWithHashing = base.ConstructAssetBundleName(assetGroup, schema, info, assetBundleName);

            if (assetGroup == null)
            {
                Debug.Log($"[BuildScriptPackedMode_Distribute.ConstructAssetBundleName]:{assetGroup}==>{bundleNameWithHashing}");
                return bundleNameWithHashing;
            }

            // ������Դ�ַ���Ϣ --ʹ�÷ַ�ǰ׺----------------------------------------------------------------------------------
            var schema_dis = assetGroup.GetSchema<AssetGroupSchema_FixMode>();
            if (schema_dis != null)
            {
                var type = schema_dis.Type;
                bundleNameWithHashing = $"{AddressablePro.PrefixOf(type)}{bundleNameWithHashing}";
            }
            Debug.Log($"[BuildScriptPackedMode_Distribute.ConstructAssetBundleName]:{assetGroup}==>{bundleNameWithHashing}");

            return bundleNameWithHashing;
        }

        public override void ClearCachedData()
        {
            base.ClearCachedData();

            // ���� Remote��������ļ���.....
            string path = Path.Combine("ServerData");
            if (Directory.Exists(path))
            {
                try
                {
                    Directory.Delete(path, true);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }
        }

        protected override TResult DoBuild<TResult>(AddressablesDataBuilderInput builderInput, AddressableAssetsBuildContext aaContext)
        {
            var result = base.DoBuild<TResult>(builderInput, aaContext);

            var remotepath = builderInput.AddressableSettings.RemoteCatalogBuildPath.GetValue(builderInput.AddressableSettings);
			
			Debug.Log($"[BuildScriptPackedMode_Distribute.DoBuild]:buildPath==========>{Addressables.BuildPath}");
			Debug.Log($"[BuildScriptPackedMode_Distribute.DoBuild]:remotepath==========>{remotepath}");

            _CollectBundles(Addressables.BuildPath, remotepath);

            return result;
        }

        void _CollectBundles(string localpath, string remotepath)
        {
            Debug.Log($"[BuildScriptPackedMode_Distribute._CollectBundles]:......................BEGIN");

            string outpath = PATH_BUNDLE_OUT;
            if (Directory.Exists(outpath))
            {
                try
                {
                    Directory.Delete(outpath, true);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }

            //1  copy LibaryĿ¼catalog.jso
            Shell.CopyFile(Path.Combine(localpath, "catalog.json"), Path.Combine(outpath, "catalog.json"));

            //2  copy localpath Ŀ¼
            Shell.CopyFilesInDirectoty(localpath, outpath,"*.bundle");

            //3  copy remotepath Ŀ¼..
            Shell.CopyFilesInDirectoty(remotepath, outpath, "*.bundle");

            //4  copy version.info Ŀ¼..
            Shell.CopyFile(Path.Combine(Application.streamingAssetsPath, "version.info"), Path.Combine(outpath, "_version.info"));


            //5.copy HotAssets ��������ʲ�...
            Shell.CopyDirectory(Builder.PATH_HOTASSETS, outpath);



            Debug.Log($"[BuildScriptPackedMode_Distribute._CopyFile]:Success@ {outpath}");

            Debug.Log($"[BuildScriptPackedMode_Distribute._CopyFile]:......................END");
        }
       



    }

}

